/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cannes.dao.implement;

import cannes.bean.planning.Film;
import cannes.connexion.ConnexionBDD;
import cannes.dao.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author p1000666
 */
public class FilmDAO extends DAO<Film> {

    public FilmDAO(Connection conn) {
        super(conn);
    }

    @Override
    public boolean create(Film obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Film obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Film obj) {
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "DELETE FROM film WHERE filmid = " + obj.getId());
            result.rowDeleted();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }

    @Override
    public int next() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Film find(int id) {
        Film film = new Film();

        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM film WHERE filmid = " + id);
            if (result.first()) {
                film = new Film(result.getInt("filmid"), result.getString("titre"),
                        result.getString("resume"), result.getDate("annee"),
                        result.getDouble("duree"), result.getString("categorie"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return film;

    }

    @Override
    public ArrayList<Film> findAll() {
                
        ArrayList<Film> listeFilm = new ArrayList<Film>();
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM film");
            while(result.next()){
                listeFilm.add(new Film(result.getInt("filmid"), result.getString("titre"),
                        result.getString("resume"), result.getDate("annee"),
                        result.getDouble("duree"), result.getString("categorie")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeFilm;
        
    }
    
    public ArrayList<Film> findByCategorie(String cat) {
                
        ArrayList<Film> listeFilm = new ArrayList<Film>();
        
        try {
            ResultSet result = this.connect.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT * FROM film WHERE categorie = '"+cat+"'");
            while(result.next()){
                listeFilm.add(new Film(result.getInt("filmid"), result.getString("titre"),
                        result.getString("resume"), result.getDate("annee"),
                        result.getDouble("duree"), result.getString("categorie")));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return listeFilm;
        
    }
}
